using System;
using System.GunSystem;
using Model;
using QFramework;
using Query;
using UnityEngine;

namespace ViewController.UI
{
    public class UIController : MonoBehaviour, IController
    {
        /// <summary>
        ///     自定义字体大小
        /// </summary>
        private readonly Lazy<GUIStyle> _labelStyle = new(() => new GUIStyle(GUI.skin.label)
        {
            fontSize = 40
        });

        private IGunSystem _gunSystem;

        private int _maxBulletCount;

        private IPlayerModel _playerModel;
        private IStatSystem _statSystem;

        private void Awake()
        {
            _statSystem = this.GetSystem<IStatSystem>();
            _playerModel = this.GetModel<IPlayerModel>();
            _gunSystem = this.GetSystem<IGunSystem>();

            // 查询当前枪械弹夹容量
            _maxBulletCount = this.SendQuery(new MaxBulletCountQuery(_gunSystem.CurrentGun.Name.Value));

            this.RegisterEvent<OnCurrentGunChanged>(e =>
            {
                _maxBulletCount = this.SendQuery(new MaxBulletCountQuery(e.Name));
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        private void OnDestroy()
        {
            _statSystem = null;
            _playerModel = null;
            _gunSystem = null;
        }

        private void OnGUI()
        {
            if (_gunSystem == null) return;

            GUI.Label(new Rect(10, 10, 400, 100), $"生命:{_playerModel.Hp.Value}/3", _labelStyle.Value);
            GUI.Label(new Rect(10, 60, 400, 100),
                $"枪内子弹:{_gunSystem.CurrentGun.BulletCountInGun.Value}/{_maxBulletCount}",
                _labelStyle.Value);
            GUI.Label(new Rect(10, 110, 400, 100), $"枪外子弹:{_gunSystem.CurrentGun.BulletCountOutGun.Value}",
                _labelStyle.Value);
            GUI.Label(new Rect(10, 160, 400, 100), $"枪械名字:{_gunSystem.CurrentGun.Name.Value}",
                _labelStyle.Value);
            GUI.Label(new Rect(10, 210, 400, 100), $"枪械状态:{_gunSystem.CurrentGun.GunState.Value}",
                _labelStyle.Value);
            GUI.Label(new Rect(Screen.width - 10 - 300, 10, 300, 100), $"击杀数量:{_statSystem.KillCount.Value}",
                _labelStyle.Value);
        }

        public IArchitecture GetArchitecture()
        {
            return ShootingEditor2D.Interface;
        }
    }
}